/*******************************************************************************
 * Copyright (c) 2011 Grupo de Sistemas Inteligentes (GSI) - DIT UPM
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/
package es.upm.dit.gsi.jadex.customproject.wizards;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExecutableExtension;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.Action;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IObjectActionDelegate;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.dialogs.WizardNewProjectCreationPage;
import org.eclipse.ui.progress.WorkbenchJob;
import org.eclipse.ui.wizards.newresource.BasicNewProjectResourceWizard;
import org.maven.ide.eclipse.actions.UpdateSourcesAction;

import es.upm.dit.gsi.eclipse.jadex.util.ImageFactory;

/**
 * This wizard is able to create a new Jadex project
 * 
 * @author Pablo Muñoz
 */
public class NewJadexProjectWizard extends Wizard implements INewWizard,
IExecutableExtension {
	
	private static final String WIZARD_NAME = "New Jadex Project"; //$NON-NLS-1$
	
	private static final String PAGE_NAME = "New Jadex Project Wizard"; //$NON-NLS-1$

	
	private WizardNewProjectCreationPage firstPage;
	
	private IConfigurationElement _configurationElement;

	public NewJadexProjectWizard() {
		setWindowTitle(WIZARD_NAME);
		setDefaultPageImageDescriptor(ImageFactory.getImageDescriptor(ImageFactory.GSI_BANNER_SMALL));
	}

	
	@Override
	public void addPages() {
	    super.addPages();
	    
	    firstPage = new WizardNewProjectCreationPage(PAGE_NAME);
	    firstPage.setTitle(NewWizardMessages.NewJadexProjectWizard_0);
	    firstPage.setDescription(NewWizardMessages.NewJadexProjectWizard_1);
	    addPage(firstPage);
	}

	
	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean performFinish() {
	    String name = firstPage.getProjectName();
	    IPath location = null;
	    if (!firstPage.useDefaults()) {
	        location = firstPage.getLocationPath();
	    } // else location == null
	    
	    final IProject newProject = JadexProjectSupport.createProject(name, location, null);
	    final Shell shell = Display.getCurrent().getActiveShell();
	    BasicNewProjectResourceWizard.updatePerspective(_configurationElement);
	   
	    WorkbenchJob job = new WorkbenchJob(Display.getCurrent(),"lala") {
			
			@Override
			public IStatus runInUIThread(IProgressMonitor monitor) {
				final Action action = new Action() {};
	  			IObjectActionDelegate updateAction = new UpdateSourcesAction(shell);
	  			updateAction.selectionChanged(action, new StructuredSelection(newProject));
	  			updateAction.run(action);
			return Status.OK_STATUS;			}
		};
		job.setSystem(true);
		job.schedule(1000);
		return true;
	}


	@Override
	public void setInitializationData(IConfigurationElement config, String propertyName, Object data) throws CoreException {
	    _configurationElement = config;
	}
}
